package com.tobdev.qywxinner.config;


import com.tobdev.qywxinner.intercepter.TokenFilter;
import com.tobdev.qywxinner.utils.JWTUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {


//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**").allowedOrigins("http://www.fjjscl.com", "http://localhost") // 不可使用 "*"
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("Authorization", "authorization")

    /// /                .exposedHeaders("Authorization", "Content-Disposition")
//                .allowCredentials(true) // 建议加上
//                .maxAge(3600);
//
//    }
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 支持携带cookie
        config.addAllowedOrigin("http://www.fjjscl.com");
        config.addAllowedOrigin("http://localhost");
        config.addAllowedHeader("Authorization");
        config.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        CorsFilter corsFilter = new CorsFilter(source);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(corsFilter);
        bean.setOrder(0); // 优先执行，保证CORS头先添加
        return bean;
    }

    @Bean
    public FilterRegistrationBean<TokenFilter> tokenFilter(JWTUtils jwtUtils) {
        FilterRegistrationBean<TokenFilter> registration = new FilterRegistrationBean<>();
        // 通过构造器注入 JWTUtils
        registration.setFilter(new TokenFilter(jwtUtils));
        registration.addUrlPatterns("/api/*", "/contact/departmentList/*");
        registration.setOrder(1);
        return registration;
    }
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //方式一 跨域
//        //https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-cors
//        //todo 太随意
//        registry.addMapping("/**")
//                .allowedOrigins("http://www.fjjscl.com") // 或指定你的前端 origin
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
//                .allowedHeaders("*") // 或 .allowedHeaders("Authorization", ...)

    /// /                .allowedHeaders("Authorization")
//                .allowCredentials(true)
//                .maxAge(3600);
//
//        // Add more mappings...
//    }
//
//    @Bean
//    public HttpMessageConverter responseBodyConverter() {
//        //解决返回值中文乱码
//        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
//        return converter;
//    }
//
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(responseBodyConverter());
//    }
//
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        //配置图片静态资源处理器
//        registry.addResourceHandler("/WW_verify_BdahsMA9ehRZafdD.txt")
//                .addResourceLocations("classpath:/static/WW_verify_BdahsMA9ehRZafdD.txt");
//    }


}